let cityVos = [];

layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    zmnLayUtil.layFormVerify(form);

    form.on('submit(set-ratio-batch)', function (obj) {
        cityVos = [];
        $("input:checkbox[name='areaId']:checked").each(function (i) {
            let msg = $(this).attr('citymsg').split(",");
            cityVos.push({
                "provinceId": msg[0], "provinceName": msg[1], "provinceCode": msg[2],
                "cityId": msg[3], "cityName": msg[4], "cityCode": msg[5]
            })
        });

        if (cityVos.length == 0) {
            layer.msg("请选择城市！");
            return false;
        }

        zmnLayUtil.openIframeCustom(layer, "/channel/citypriceratio/cityBatchRatio.action?channelId=" + json.channelId + "&bizType=" + json.bizType + "&priceType=" + json.priceType, {
            title: "默认系数设置",
            shade: 0.9,
            area: ['600px', '500px']
        });
        return false;

    });

    form.on('submit(categ-ratio-batch)', function (obj) {
        let cityIdArr = [];
        $("input:checkbox[name='areaId']:checked").each(function (i) {
            let msg = $(this).attr('citymsg').split(",");
            if (msg.length > 3 && zmnUtil.isNotNull(msg[3])) {
                cityIdArr.push(msg[3]);
            }
        });

        if (cityIdArr.length == 0) {
            layer.msg("请选择城市！");
            return false;
        }

        top.layui.index.openTabsPage("/channel/citypriceratio/categoryRatioBatch.action?cityIdStr=" + cityIdArr.join(",") + "&priceType=" + json.priceType + "&bizType=" + json.bizType + "&channelId=" + json.channelId, "设置分类系数");
        return false;

    });

    form.on('checkbox(checkAll)', function (data) {
        var _class = data.value;
        if (data.elem.checked) {
            $("." + _class).each(function () {
                $(this).prop("checked", true);
            });
            form.render('checkbox');
        } else {
            $("." + _class).each(function () {
                $(this).prop("checked", false);
            });
            form.render('checkbox');
        }
        form.render('checkbox');
    });

    var checkAll = function (data) {
        var _class = data.value.substr(0, data.value.lastIndexOf("-"));
        var item = $("." + _class);
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == false) {
                $("input[value='" + _class + "'").prop("checked", false);
                form.render('checkbox');
                return;
            }
        }
        //如果都勾选了  勾上全选
        var all = item.length;
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == true) {
                all--;
            }
        }
        if (all == 0) {
            $("input[value='" + _class + "'").prop("checked", true);
            form.render('checkbox');
        }
    };

    //有一个未选中全选取消选中
    form.on('checkbox(checkOne)', checkAll);

    $(".checkAll").each(function () {
        checkAll({value: this.value + "-0"});
    })

    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }

    //对外暴露的接口
    exports('price_ratio_city_batch', {});
});

function submitChannelCityBatchRatio(channelId, cityRatio, cityMeterRatio, bizType, priceType, effectiveDate) {
    let data = {};
    data.provinceCityVOS = cityVos;
    data.channelId = channelId;
    let url = '/channel/citypriceratio/saveCityRatioBatch.action';
    data.toBeEffectiveRatio = cityRatio;
    data.meterToBeEffectiveRatio = cityMeterRatio;
    data.bizType = bizType;
    data.priceType = priceType;

    if (effectiveDate == null || effectiveDate == '' || effectiveDate == undefined) {
        var nowDate = new Date();
        data.effectiveDate = new Date(nowDate.getTime() + 24 * 60 * 60 * 1000).format("yyyy-MM-dd");
    } else {
        data.effectiveDate = effectiveDate;
    }

    let index;
    layui.$.ajax({
        async: false,
        type: "POST",
        url: url,
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(data),
        beforeSend: function () {
            index = layui.layer.load(1, {
                shade: [0.5, '#DBDBDB']
            });
        },
        success: function (json) {
            if (json.status == 200) {
                layui.layer.msg(json.message);
                layui.layer.close(index);
                return false;
            } else {
                layui.layer.msg(json.message, {icon: 5});
                layui.layer.close(index);
                return false;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layui.layer.close(index);
            return false;
        }
    });
    return false;
}